package com.example.wanglong.myoneapplication.activity;
import com.example.wanglong.myoneapplication.activity.TitleChange;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.example.wanglong.myoneapplication.viewmodel.AdInfo;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.example.wanglong.myoneapplication.common.BaseActivity;
import com.example.wanglong.myoneapplication.R;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.example.wanglong.myoneapplication.adbanner.ClickableImageView;
import com.example.wanglong.myoneapplication.utils.CommonUtil;
import com.example.wanglong.myoneapplication.adbanner.VisaImageCycleView;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {

    private LinearLayout v;
    private ScrollView mScrollView;
    private RelativeLayout usa;
    VisaImageCycleView imageCycleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        initAD();
        //初始化
        initIndex();
        //TitleChange.init();
    }

    //初始化
    public void initIndex(){

        v =(LinearLayout) findViewById(R.id.visatitle);
        v.getBackground().setAlpha(0);

        usa=(RelativeLayout) findViewById(R.id.visathree);

        //滑动title透明度变化
        initChangeTitle();

        usa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                actionStart(MainActivity.this,"美国");
            }
        });


    }




    public static void actionStart(Context context,String city){
        Intent intent=new Intent(context,ListActivity.class);
        intent.putExtra("city", city);
        context.startActivity(intent);
    }

    //滑动title透明度变化
    public void initChangeTitle(){
        mScrollView=(ScrollView) findViewById(R.id.scrollvisacontent);
        mScrollView.setOnTouchListener(new TouchListenerImpl());
        final ViewTreeObserver.OnScrollChangedListener listener = new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                System.out.println(mScrollView.getScrollY());
                int sy=mScrollView.getScrollY();
                if(sy>=0 && sy<=255){
                    v.getBackground().setAlpha(sy);
                } else if(sy>255){
                    v.getBackground().setAlpha(255);
                }
            }
        };
        ViewTreeObserver observer = mScrollView.getViewTreeObserver();
        observer.addOnScrollChangedListener(listener);
    }

    //监控touch事件title透明度变化
    private class TouchListenerImpl implements View.OnTouchListener {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            //System.out.println(view.getScrollY());
            int sy=view.getScrollY();
            if(sy>=0 && sy<=255){
                v.getBackground().setAlpha(view.getScrollY());
            } else if(sy>255){
                v.getBackground().setAlpha(255);
            }
            return false;
        }

    }

    //初始化首页广告
    public void initAD() {

        // 初始化ImageLoader
        @SuppressWarnings("deprecation")
        DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.dsbb) // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.dsbb) // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.dsbb) // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true) // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
                .build(); // 创建配置过得DisplayImageOption对象

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).defaultDisplayImageOptions(options)
                .threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).build();
        ImageLoader.getInstance().init(config);


        imageCycleView = (VisaImageCycleView) findViewById(R.id.indexCycleView);
        AdInfo adInfo = new AdInfo();
        ArrayList<String> imageUrlList = new ArrayList<String>();
        ArrayList<String> clickUrlList = new ArrayList<String>();
        ArrayList<String> advertisementPosition = new ArrayList<String>();
        //简析报文
        for (int i = 0; i < 3; i++) {
            switch (i){
                case 0:
                    imageUrlList.add("drawable://"+R.drawable.yinghua750_360);
                    clickUrlList.add("photo"+i);
                    advertisementPosition.add(String.valueOf(i));
                    break;
                case 1:
                    imageUrlList.add("drawable://"+R.drawable.online_visa750_360);
                    clickUrlList.add("photo"+i);
                    advertisementPosition.add(String.valueOf(i));
                    break;
                case 2:
                    imageUrlList.add("drawable://"+R.drawable.taiwan750_360);
                    clickUrlList.add("photo"+i);
                    advertisementPosition.add(String.valueOf(i));
                    break;
            }
        }
        adInfo.ClickUrlList = clickUrlList;
        adInfo.ImageUrlList = imageUrlList;
        adInfo.AdvertisementPositionList = advertisementPosition;

        initCarsuelView(adInfo.ClickUrlList, adInfo.ImageUrlList);


    }

    //初始化轮播图
    public void initCarsuelView(final ArrayList<String> imageDescList, ArrayList<String> urlList) {

        LinearLayout.LayoutParams cParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, CommonUtil.getScreenWidth(MainActivity.this) * 35 / 65);
        imageCycleView.setLayoutParams(cParams);



        VisaImageCycleView.ImageCycleViewListener mAdCycleViewListener = new VisaImageCycleView.ImageCycleViewListener() {
            @Override
            public void onImageClick(int position, View imageView) {
                /**实现点击事件*/
                String url = imageDescList.get(position);

                Toast.makeText(MainActivity.this,url,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void displayImage(String imageURL, ImageView imageView) {
                ImageLoader.getInstance().displayImage(imageURL, imageView);
            }
        };
        /**设置数据*/
        imageCycleView.setImageResources(imageDescList, urlList, mAdCycleViewListener);
        imageCycleView.startImageCycle();
    }



}
